﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using Ext.Net;

namespace LongSpeed.WebSite.Main
{
    public partial class ServerConfigList : Behaviors.Session
    {
        private int parentId
        {
            get { return Convert.ToInt32(Request.QueryString["mPartId"]); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindServerConfig(parentId, false);
            }
        }

        Behaviors.Memu _ParentMemu;
        private Behaviors.Memu ParentMemu
        {
            get
            {
                if (_ParentMemu == null)
                {
                    Behaviors.Memu pm = new Behaviors.Memu();
                    pm.Init(this.parentId);

                    _ParentMemu = pm;
                }

                return _ParentMemu;
            }
        }

        IList<Behaviors.ServerConfig> _ServerConfigData;
        IList<Behaviors.ServerConfig> ServerConfigData
        {
            get
            {
                if (_ServerConfigData == null)
                {
                    LongSpeed.WebSite.Behaviors.ServerConfig sc = new LongSpeed.WebSite.Behaviors.ServerConfig();
                    _ServerConfigData = sc.GetServersByParent(ParentMemu);
                }

                return _ServerConfigData;
            }
            set { _ServerConfigData = value; }
        }

        [Ext.Net.DirectMethod]
        public void BindServerConfig(int parentId, bool refresh)
        {
            if (refresh)
            {
                this.ServerConfigData = null;
            }
            this.plParentMemu.Html = string.Format("<p style='padding: 10px 10px 10px 10px; color: #556677; font-size: 15px'>所属菜单：{0}</p>", ParentMemu.Name);

            this.stServerConfig.DataSource = this.ServerConfigData;
            this.stServerConfig.DataBind();
        }

        protected void RefreshServerConfigData(object sender, StoreRefreshDataEventArgs e)
        {
            BindServerConfig(parentId, true);
        }

        [DirectMethod]
        public int SaveServerConfig(object obj)
        {
            Ext.Net.JsonReader jo = new JsonReader();
            var sc = Newtonsoft.Json.JsonConvert.DeserializeObject(obj.ToString(), typeof(Behaviors.ServerConfig)) as Behaviors.ServerConfig;
            sc.ParentMemu = new Behaviors.Memu() { ID = parentId };
            if (sc.Save()) return sc.ID; else return 0;
        }
        [DirectMethod]
        public bool DeleteServerConfig(object obj)
        {
            Ext.Net.JsonReader jo = new JsonReader();
            var sc = Newtonsoft.Json.JsonConvert.DeserializeObject(obj.ToString(), typeof(Behaviors.ServerConfig)) as Behaviors.ServerConfig;
            return sc.Delete();
        }

    }
}